package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName HospitalSetController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-09-29 17:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin //跨域
@Api("医院设置接口")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //根据医院编号查询医院设置
    @GetMapping("getHospitalSetByHoscode/{hoscode}")
    public HospitalSet getHospitalSet(@PathVariable String hoscode){
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = hospitalSetService.getOne(queryWrapper);
        return hospitalSet;
    }

    //查询所有医院设置
    @GetMapping("findAll")
    @ApiOperation("医院设置列表")
    public R findAll(){
        List<HospitalSet> list = hospitalSetService.list();

        //制造异常
       /* try {
            int a = 10/0;
        }catch (Exception e){
            throw new YyghException(20001,"出现自定义异常");
        }*/


        //对结果进行非空判断
        if (list.size() == 0) {
            R r = new R();
            r.setCode(20001);
            r.setSuccess(false);
            r.setMessage("医院设置列表为空,请添加数据");
            return r;
        }
        return R.ok().data("list",list);
    }

    //逻辑删除
    @DeleteMapping("{id}")
    @ApiOperation("医院设置删除")
    public R removeById(@ApiParam(name = "id",value = "医院设置编号") @PathVariable Long id){
        boolean b = hospitalSetService.removeById(id);
        return b?R.ok():R.error();
    }

    @GetMapping("{page}/{limit}")
    @ApiOperation("无条件分页查询")
    public R pageList(@ApiParam(name = "page",value = "医院设置信息页码") @PathVariable("page") Long page,@ApiParam("医院设置信息每页信息条数") @PathVariable("limit") Long limit){
        Page<HospitalSet> pageParam = new Page<>(page,limit);
        hospitalSetService.page(pageParam,null);

        List<HospitalSet> rows = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total",total).data("rows",rows);
    }

    @PostMapping("{page}/{limit}")
    @ApiOperation("按条件分页查询")
    public R pageQuery(@ApiParam("医院设置信息页码") @PathVariable Long page, @ApiParam("医院设置信息每页信息条数") @PathVariable Long limit,@ApiParam("医院设置信息查询条件") @RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> pageParam = new Page<>(page,limit);

        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

        if (hospitalSetQueryVo != null) {
            String hosname = hospitalSetQueryVo.getHosname();
            String hoscode = hospitalSetQueryVo.getHoscode();

            if (hosname != null) {
                queryWrapper.like("hosname",hosname);
            }
            if (hoscode != null) {
                queryWrapper.eq("hoscode",hoscode);
            }
        }
        hospitalSetService.page(pageParam,queryWrapper);

        List<HospitalSet> rows = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total",total).data("rows",rows);
    }

    @PostMapping("saveHospSet")
    @ApiOperation("给医院设置新增医院")
    public R save(@RequestBody HospitalSet hospitalSet){
        //检查医院编号是否存在
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode",hospitalSet.getHoscode());
        int count = hospitalSetService.count(queryWrapper);
        if (count > 0){
            throw new YyghException(20001,"医院编号重复");
        }
        //设置状态, 1 正常, 0 锁定
        boolean b = hospitalSetService.save(hospitalSet);

        return b?R.ok():R.error();
    }

    @GetMapping("getHospSet/{id}")
    @ApiOperation("根据id值查询医院设置")
    public R getById(@ApiParam("医院设置信息查询id")@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet == null) {
            return  R.error();
        }
        return R.ok().data("item",hospitalSet);
    }

    @PostMapping("updateHospSet")
    @ApiOperation("根据id值修改医院设置")
    public R updateById(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b?R.ok():R.error();
    }

    @DeleteMapping("batchRemove")
    @ApiOperation("批量删除医院设置信息")
    public R batchRemove(@RequestBody List<Long> ids){
        boolean b = hospitalSetService.removeByIds(ids);
        return b?R.ok():R.error();
    }

    @PutMapping("lockHospitalSet/{id}/{status}")
    @ApiOperation("根据id修改医院设置状态")
    public R lockHospitalSet(@ApiParam("医院设置id") @PathVariable Long id,@ApiParam("医院设置状态信息") @PathVariable Integer status){
        //status值校验
        if (status != 0 && status != 1) {
            throw new RuntimeException("status值只能为0或1");
        }

        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);

        //调用方法
        boolean b = hospitalSetService.updateById(hospitalSet);

        return b?R.ok():R.error();
    }
}